package com.uh.calculation.batchOperator;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uh.calculation.bean.*;
import com.uh.calculation.util.FormulaBatchUtil;
import com.uh.calculation.util.FormulaUtil;
import com.uh.calculation.util.GetBean;
import com.uh.calculation.util.TimeUtil;
import com.uh.microservice.core.dao.JdbcDao;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

//import com.uh.calculation.util.GetFileds;

@Data
/**
 * 同比环比增长算子(单字段变更)
 */
public class XsgBathOperator {
    private JdbcDao jdbcDao;

    private WarnJiSuanBean warnJiSuanBean;

    private SqlWriteBean sqlWriteBean;

    public XsgBathOperator(JdbcDao jdbcDao, WarnJiSuanBean warnJiSuanBean, SqlWriteBean sqlWriteBean){
        this.jdbcDao=jdbcDao;
        this.warnJiSuanBean=warnJiSuanBean;
        this.sqlWriteBean=  sqlWriteBean;
    }

    /**
     * 计算小升规预警因子
     */
    public void jisuanFuwu(JSONArray ls, List<OperatorVal> list) {
        if (ls == null && ls.size() == 0) {
            return;
        }
        for (OperatorVal operatorVal : list) {
            List<FormulaBean> formulaBeans = operatorVal.getFormulaBeans();//计算公式
            String field = operatorVal.getField();
            List<TbFeild> tbFeilds = GetBean.getTbf(field);
            String f1 = tbFeilds.get(0).getFieldName();//获取行业字段
            Date lastTime = warnJiSuanBean.getLastTime();
            //WarnJiSuanBean warnJiSuanBean = new WarnJiSuanBean();
            for (int i = 0; i < ls.size(); i++) {
                try {
                    JSONObject object = ls.getJSONObject(i);
                    String timeCode = object.get("time_code").toString();
                    //时间游标判断，及核查规则是否符合
                    if (TimeUtil.bj(lastTime, timeCode, sqlWriteBean.getYear_is_month(), sqlWriteBean.getIs_hc())) {
                        String val1 = object.get(f1).toString();
                        //根据当前计算类解析规则  将数据和字段对应
                        tbFeilds.get(0).setData(val1);
                        //扔进公式解析工具
                        sqlWriteBean.setDimension(operatorVal.getDimension());
                        sqlWriteBean.setTimeCode(timeCode);
                        new FormulaBatchUtil(tbFeilds, formulaBeans, warnJiSuanBean, sqlWriteBean);
                    }
                } catch (Exception e) {

                }

            }
        }
    }

    /**
     * 计算工业行业小升规预警因子
     */
    public void jisuanMaoyi(List<Map<String, Object>> ls, List<OperatorVal> list) {
        if (ls == null ||ls.size() == 0) {
            return;
        }
        for (OperatorVal operatorVal : list) {
            List<FormulaBean> formulaBeans = operatorVal.getFormulaBeans();//计算公式
            String field = operatorVal.getField();
            List<TbFeild> tbFeilds = GetBean.getTbf(field);
            String f1 = tbFeilds.get(0).getFieldName();//获取行业字段
            String f2 = tbFeilds.get(1).getFieldName();//获取营业额字段
            Date lastTime = warnJiSuanBean.getLastTime();
            for (int i = 0; i < ls.size(); i++) {
                String timeCode = ls.get(i).get("time_code").toString();
                //时间判断
                try {
                    if (lastTime != null) {

                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                        Date d = sdf.parse(timeCode);
                        if (d.before(lastTime)) {
                            continue;
                        }

                    }
                    String val1 = ls.get(i).get(f1).toString();
                    String val2 = ls.get(i).get(f2).toString();
                    //根据当前计算类解析规则  将数据和字段对应
                    tbFeilds.get(0).setData(val1);
                    tbFeilds.get(1).setData(val2);
                    //扔进公式解析工具
                    new FormulaUtil(tbFeilds, formulaBeans, warnJiSuanBean, operatorVal.getDimension(), timeCode);
                } catch (Exception e) {
                }
            }
        }
    }

    public void jisuanMaoyiZc(JSONArray ls, List<OperatorVal> list) {
        if (ls == null || ls.size() == 0) {
            return;
        }
        for (OperatorVal operatorVal : list) {
            List<FormulaBean> formulaBeans = operatorVal.getFormulaBeans();//计算公式
            String field = operatorVal.getField();
            List<TbFeild> tbFeilds = GetBean.getTbf(field);
            String f1 = tbFeilds.get(0).getFieldName();
            Date lastTime = warnJiSuanBean.getLastTime();
            for (int i = 0; i < ls.size(); i++) {
                JSONObject object = ls.getJSONObject(i);
                String timeCode = object.get("time_code").toString();
                //时间判断
                try {
                    if (lastTime != null) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                        Date d = sdf.parse(timeCode);
                        if (d.before(lastTime)) {
                            continue;
                        }
                    }
                    String val1 = object.get(f1).toString();
                    //根据当前计算类解析规则  将数据和字段对应
                    tbFeilds.get(0).setData(val1);
                    //扔进公式解析工具
                    new FormulaBatchUtil(tbFeilds, formulaBeans, warnJiSuanBean, sqlWriteBean);
                } catch (Exception e) {
                }
            }
        }
    }
}
